// pages/userInfo/update/update.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo()
  },

  //获取用户个人信息
  getUserInfo: function () {
    var that = this;
    wx.request({
      url: app.host + '/user/info/',
      header: {
        accessToken: app.accessToken
      },
      method: 'GET',
      complete: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.setData({
            userInfo: res.data.data
          })
        }
        wx.hideLoading()
      }
    })
  },


  formSubmit:function(res){
    console.log(res)
    var that = this;
    var fromData = res.detail.value

    //判断真实姓名是否正确
    if (!this.checkHan(fromData.realName)){
      wx.showToast({
        title: '真实姓名格式不正确!',
        icon: 'none'
      })
      return
    }

    //判断手机号码格式是否正确
    if (!this.checkPhone(fromData.phone)) {
      wx.showToast({
        title: '手机号码格式不正确!',
        icon: 'none'
      })
      return
    }
    //判断邮箱格式是否正确
    if (!this.checkEmail(fromData.email)) {
      wx.showToast({
        title: '邮箱格式不正确!',
        icon: 'none'
      })
      return
    }
    wx.request({
      url: app.host + '/user/update',
      header: {
        accessToken: app.accessToken
      },
      data: fromData,
      method: 'post',
      complete: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          wx.reLaunch({
            url: '/pages/index/index',
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
        wx.hideLoading()
      }
    })
  },

  checkEmail: function (str) {
    var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    if (re.test(str)) {
      return true
    }
    return false
  },
  checkPhone: function (str) {
    var re = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (re.test(str)) {
      return true
    }
    return false
  },
  checkHan:function (str){
    var re =  /^[\u4e00-\u9fa5]+$/ ;
    if (re.test(str)) {
      return true
    }
    return false
  }

})